<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\CardList;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;
use function foo\func;

class CardListController extends AdminController
{


    public function index(Content $content)
    {
        return $content->title('课时卡')
            ->description('列表')
            ->body($this->grid());
    }

    public function create(Content $content)
    {
        return $content->title('课时卡')
            ->description('新增')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new CardList(), function (Grid $grid) {
//            $grid->fixColumns(1, -1);

            $grid->column('id')->sortable();
            $grid->column('card_name');
            $grid->column('card_img','封面图')->image('','100','100');
            $grid->column('card_price');
            $grid->column('time','有效时长')->display(function ($value){
                return $value;
//               $time = 365 - ceil( (time() - $this->created_at) / 86400);
//               return $time;
            });
            $grid->column('class_intro');

            $grid->column('card_in_img','详情图')->image('','100','100');


            $grid->column('created_at')->display(function ($value){
                return date('Y-m-d H:i:s',$value);
            });

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });

            $grid->actions(function (Grid\Displayers\Actions $actions) {
//                $actions->disableDelete();
//                $actions->disableEdit();
//                $actions->disableQuickEidt();
                $actions->disableView();
            });

        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new CardList(), function (Form $form) {
            $form->display('id');
            $form->text('card_name');
            $form->file('card_img','封面图')->autoUpload()->uniqueName();
            $form->text('card_price')->help('默认元为单位');
            $form->text('time')->help('默认天为单位');
            $form->text('class_intro');
            $form->file('card_in_img','课程详情图')->autoUpload()->uniqueName();
//            $form->text('created_at');
        });
    }
}
